package cn.spike.basic.util;

import cn.spike.basic.jwt.JwtUtils;
import cn.spike.basic.jwt.LoginData;
import cn.spike.basic.jwt.Payload;
import cn.spike.basic.jwt.RsaUtils;
import cn.spike.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

/**
 * 工具类：获取登陆人信息 - 审核的审核要获取当前审核人就是登陆人的id
 * @author:spike
 * @create:2023-02-17 10:21
 */
public class LoginContext {

    public static Logininfo getLogininfo(HttpServletRequest request){
        try {
            //获取请求头中的jwtToken
            String jwtToken = request.getHeader("token");
            //获取公钥对象
            PublicKey publicKey = RsaUtils.getPublicKey(RsaUtils.class.getClassLoader().getResource("auth_rsa.pub").getFile());
            //使用工具类解密 - 使用公钥解密
            Payload<LoginData> payload = JwtUtils.getInfoFromToken(jwtToken, publicKey, LoginData.class);
            //返回登陆人信息
            return payload.getLoginData().getLogininfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
